import { dayjs } from "element-plus";

/**
 * 字段内容翻译
 */
const handleTranslate = (list, value) => {
  const obj = list.find((item) => item.value == value);
  return typeof obj === "undefined" ? "" : obj.label;
};
// 下载文件
const downloadFile = (data, name) => {
  const link = document.createElement("a");
  const blob = new Blob([data], { type: "application/vnd.ms-excel" });
  link.style.display = "none";
  link.href = URL.createObjectURL(blob);
  if (name=="事件上报模板"){
 link.setAttribute("download", `${name}.xlsx`);
  }else{
 link.setAttribute("download", `${name}${dayjs().format("YYYY-MM-DD")}.xlsx`);
  }
   
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};
// id命中对应事件类型name
const findTypeName = (data, id, path = []) => {
  for (let i = 0; i < data.length; i++) {
    path.push(data[i].name);
    if (data[i].id == id) {
      return path;
    }
    if (data[i].children && data[i].children.length) {
      const foundPath = findTypeName(data[i].children, id, path);
      if (foundPath) {
        return foundPath;
      }
    }
    path.pop();
  }
  return null;
};
export { handleTranslate, downloadFile, findTypeName };
